package com.scu.controller;

import com.scu.pojo.Comment;
import com.scu.pojo.Topic;
import com.scu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    //发表评论
    @RequestMapping("postComment")
    @ResponseBody
    public Map postComment(HttpSession session,HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        Comment comment = new Comment();
        String content = request.getParameter("pcontent");
        String fid = request.getParameter("fid");
        // session 获取id
        comment.setU_id(session.getAttribute("u_id").toString());
        comment.setT_id(fid);
        comment.setC_content(content);
//        System.out.println("又要改map？");
        int result = commentService.postComment(comment);
//        System.out.println("这里没错就好");

        map.put("code",200);
        map.put("msg","添加成功");
        return map;
    }

    //修改评论
    @RequestMapping("redactComment")
    @ResponseBody
    public Map redactComment(Comment comment){
        HashMap<String,Object> map = new HashMap<>();

        int result = commentService.redactComment(comment);

        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //获取评论列表
    @RequestMapping("getCommentList")
    @ResponseBody
    public Map getCommentList(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();

        Comment comment = new Comment();
        String fid = request.getParameter("fid");
        comment.setT_id(fid);
        List<Comment> commentList = commentService.getCommentList(comment);

        map.put("code",200);
        map.put("msg","");
        map.put("data",commentList);
        return map;
    }

    //删除评论
    @RequestMapping("removeComment")
    @ResponseBody
    public Map removeComment(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        String c_id = request.getParameter("c_id");
        int result = commentService.removeComment(c_id);
        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //获取我的评论
    @RequestMapping("getMyComment")
    @ResponseBody
    public Map getMyComment(Comment comment, HttpSession session){
        HashMap<String,Object> map = new HashMap<>();

        // session 获取id
        comment.setU_id(session.getAttribute("u_id").toString());

        List<Comment> commentList = commentService.getMyComment(comment);

        map.put("code",200);
        map.put("msg","");
        map.put("data",commentList);
        return map;
    }

    //分页获取我的评论  不做了？？
    @RequestMapping("getMyCommentByPage")
    @ResponseBody
    public Map getMyCommentByPage(Comment comment,int page,int limit){
        HashMap<String,Object> map = new HashMap<>();
        System.out.println(comment.getU_id());
        System.out.println(comment.getUauthorid());
        System.out.println(comment.getC_content());

//
//        Comment comment = new Comment();
//        comment.setU_id(u_id);

//        System.out.println(comment.getC_author_id());

//        List<Comment> commentList = commentService.getMyCommentByPage(comment,page,limit);

        map.put("code",0);
        map.put("msg","");
//        map.put("data",commentList);
        return map;
    }

}



